# Signal handling routines. Contains routine called to handle
# signals and to register cleanup routines that are executed
# once a signal is received. Treats HUP, INT, TERM, and QUIT
# the same.
#
# Copyright (C) 2001, 2003 The Written Word, Inc.
# Copyright (C) 2001 The Written Word, LLC
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# $Id: mysig.py 236 2006-09-19 22:19:40Z china $

import os.path
import signal
import sys

_sighandlers = []

def init ():
  # initialize our signal handler
  signal.signal (signal.SIGINT, run_sighandlers)

def run_sighandlers (signum, frame):
  while _sighandlers:
    func, args = _sighandlers.pop (0)
    apply (func, args)

  sys.exit (1)

def register (func, *args):
  _sighandlers.insert (0, (func, args))

def unregister (rm_func):
  for func, args in _sighandlers:
    if rm_func == func:
      _sighandlers.remove ((func, args))
      break
